<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <title>replace()方法</title>
</head>

<body>
  <script>
    //替换——返回替换后的结果
    //字符串替换
    var str = "This is a box This is a box!";
    console.log(str);
    console.log(str.replace("This", "1111"));

    //正则表达式替换
    var pattern = /This/;
    // var pattern = /(?!^)This/g;//只匹配后边的This
    var str = "This is a box This is a box!";
    console.log(str);
    console.log(str.replace(pattern, "1111"));

    //案例 结合后续正则知识，可进行更为复杂的替换，具体参见后续练习
    console.log("sdafsa sdfea2s".replace(/a\ds/g,"*"));
    console.log("sdafsa sdfea2s".replace(/a\Ds/g,"*"));
    console.log("sdafsa sdfea2s".replace(/a\ws/g,"*"));
    console.log("sdafsa sdfea2s".replace(/a\Ws/g,"*"));
  </script>
</body>

</html>